package com.yanqu.road.server.cmd.activity.uniongroup;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.union.UnionActivityGroupProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionWarActivityMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = GameProtocol.S_UNION_ACTIVITY_GROUP_GET_MEMBERS, desc = "获取本商会的参赛名单")
public class UnionActivityGroupGetMemberCmd extends AbstractCommand {

    int activityId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        UnionActivityGroupProto.UnionActivityGroupGetMemberReqMsg reqMsg = UnionActivityGroupProto.UnionActivityGroupGetMemberReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();

        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if(activityInfo == null){
            activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
        }

        if (activityInfo == null) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }

        String unionUid = GameUnionActivityGroupMgr.getUnionUid(activityId, packet.getPlayerId());
        if(StringUtils.isNullOrEmpty(unionUid)){
            //没在名单或者展示期
            //ret = GameErrorCode.E_UNION_ACTIVITY_GROUP_NO_ABLE;
            //UnionActivityGroupHelper.getGroupTime(activityInfo)
            player.sendPacket(Protocol.U_UNION_ACTIVITY_GROUP_GET_MEMBERS,
                    UnionActivityGroupProto.UnionActivityGroupGetMemberRespMsg.newBuilder()
                            .setActivityId(activityId).setRet(ret));
            return;
        }

        player.sendUnionActivityGroupPacket(Protocol.C2_UNION_ACTIVITY_GROUP_GET_MEMBERS, reqMsg.toBuilder(), activityId);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_UNION_ACTIVITY_GROUP_GET_MEMBERS, UnionActivityGroupProto.UnionActivityGroupGetMemberRespMsg.newBuilder().setActivityId(activityId).setRet(ret));
        }
    }
}
